%{
#include <stdio.h>

int alpha[26];
int base;

%}

%start command

%token DIGIT LETTER EXIT

%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left MINUS  

%%                   

command:                
         |
         command statement '\n'
         |
        command error '\n'
         {
           yyerrok;
         }
	| command EXIT '\n'
		{
			return;
		}
	
	 ;

statement:    expression
         {
           printf("%d\n",$1);
         }
         |
         LETTER '=' expression
         {
           alpha[$1] = $3;
         }

         ;

expression:    '(' expression ')'
         {
           $$ = $2;
         }
         |
         expression '*' expression
         {

           $$ = $1 * $3;
         }
         |
         expression '/' expression
         {
           $$ = $1 / $3;
         }
         |
         expression '%' expression
         {
           $$ = $1 % $3;
         }
         |
         expression '+' expression
         {
           $$ = $1 + $3;
         }
         |
         expression '-' expression
         {
           $$ = $1 - $3;
         }
         |
         expression '&' expression
         {
           $$ = $1 & $3;
         }
         |
         expression '|' expression
         {
           $$ = $1 | $3;
         }
         |

        '-' expression %prec MINUS
         {
           $$ = -$2;
         }
         |
         LETTER
         {
           $$ = alpha[$1];
         }

         |
         numbers
         ;

numbers:  DIGIT
         {
           $$ = $1;
           base = ($1==0) ? 8 : 10;
         }       |
         numbers DIGIT
         {
           $$ = base * $1 + $2;
         }
         ;

%%
main()
{
 printf("\nEnter any expression: ");
 return(yyparse());
}

yyerror(s)
char *s;
{
  fprintf(stderr, "%s\n",s);
}

yywrap()
{
  return(1);
}
